#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef long long LL;

int n;
priority_queue<LL, vector<LL>, greater<LL>> heap;

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		LL x; cin >> x;
		heap.push(x);
	}

	LL ret = 0;
	while(heap.size() > 1)
	{
		LL tmp = heap.top(); heap.pop();
		tmp += heap.top(); heap.pop();
		ret += tmp;
		heap.push(tmp);
	}
	
	cout << ret << endl;

	return 0;
}